<template>
  <div>
    <h1>领取记录</h1>
    <!-- 条件搜索 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="优惠券名称">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入优惠券名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadClaimList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <div style="margin-top: 10px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="recordId" label="ID" width="80" />
        <el-table-column prop="couponName" label="优惠券名称" width="140">
            <template #default="scope">
                 <span v-html="scope.row.couponName"></span>
            </template>
        </el-table-column>
        <el-table-column prop="userName" label="领取人" width="100" />
        <el-table-column prop="createTime" label="领取时间" width="180" />
        <el-table-column prop="used" label="使用状态" width="100">
            <template #default="scope">
                <span v-if="scope.row.used == 0">未使用</span>
                <span v-if="scope.row.used == 1">已使用</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" />
      </el-table>
    </div>
    <!-- 分页 -->
    <div style="margin-top: 10px">
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div>
      <el-button type="primary" @click="onBack">返回</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getCouponRecordList } from "../api/api";
import { useRouter } from "vue-router";

const router = useRouter();

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});

//返回
const onBack = () => {
  console.log("onBack");
  router.push("/home/coupon");
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadClaimList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadClaimList();
};

const loadClaimList = () => {
  getCouponRecordList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadClaimList();
});
</script>

<style scoped></style>
<!-- 条件搜索样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
